<!DOCTYPE html>

<html>
<head>
  <title>sockets.coffee</title>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <link rel="stylesheet" media="all" href="public/stylesheets/normalize.css" />
  <link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
  <div class="container">
    <div class="page">

      <div class="header">
        
          <h1>sockets.coffee</h1>
        

        
          <div class="toc">
            <h3>Table of Contents</h3>
            <ol>
              
                
                <li>
                  <a class="source" href="database.html">
                    database.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="manager.html">
                    manager.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="routes.html">
                    routes.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="security.html">
                    security.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sockets.html">
                    sockets.coffee
                  </a>
                </li>
              
                
                <li>
                  <a class="source" href="sync.html">
                    sync.coffee
                  </a>
                </li>
              
            </ol>
          </div>
        
      </div>

      
        
        <h2 id="server-sockets">SERVER SOCKETS</h2>

        
      
        
        <p>Handles sockets communication using the module Socket.IO.</p>

        
          <div class='highlight'><pre><span class="class"><span class="keyword">class</span> <span class="title">Sockets</span></span></pre></div>
        
      
        
        <p>Require Expresser.</p>

        
          <div class='highlight'><pre>    expresser = <span class="built_in">require</span> <span class="string">"expresser"</span>
    settings = expresser.settings</pre></div>
        
      
        
        <p>Init socket connections.</p>

        
          <div class='highlight'><pre>    <span class="attribute">init</span>:<span class="function"> =&gt;</span>
        expresser.sockets.listenTo <span class="string">"disconnect"</span>, <span class="property">@onDisconnect</span>
        expresser.sockets.listenTo <span class="string">"clients:refresh"</span>, <span class="property">@onClientsRefresh</span></pre></div>
        
      
        
        <h2 id="trigger-entities-and-audit-data">TRIGGER: ENTITIES AND AUDIT DATA</h2>

        
      
        
        <p>Send updated <a href="entityObject.html">Entity Definition data</a> to the clients.</p>

        
          <div class='highlight'><pre>    <span class="attribute">sendEntityRefresh</span>: <span class="function"><span class="params">(entityDef)</span> =&gt;</span>
        expresser.sockets.emit <span class="string">"entitydata:refresh"</span>, entityDef</pre></div>
        
      
        
        <p>Send updated <a href="auditData.html">AuditData</a> values to the clients.</p>

        
          <div class='highlight'><pre>    <span class="attribute">sendAuditDataRefresh</span>: <span class="function"><span class="params">(auditData)</span> =&gt;</span>
        expresser.sockets.emit <span class="string">"auditdata:refresh"</span>, auditData</pre></div>
        
      
        
        <h2 id="trigger-server-logs-and-errors">TRIGGER: SERVER LOGS AND ERRORS</h2>

        
      
        
        <p>Send a server error JSON to the clients, containing a title and message.</p>

        
          <div class='highlight'><pre>    <span class="attribute">sendServerError</span>: <span class="function"><span class="params">(title, errorMessage)</span> =&gt;</span>
        errorMessage = <span class="string">"Unknown error"</span> <span class="keyword">if</span> <span class="keyword">not</span> errorMessage?
        expresser.sockets.emit <span class="string">"server:error"</span>, {<span class="attribute">title</span>: title, <span class="attribute">message</span>: errorMessage.toString().replace(<span class="string">":"</span>, <span class="string">" "</span>)}</pre></div>
        
      
        
        <h2 id="listen-client-commands">LISTEN: CLIENT COMMANDS</h2>

        
      
        
        <p>When user disconnects, emit an event with the new connection count to all clients.</p>

        
          <div class='highlight'><pre>    <span class="attribute">onDisconnect</span>:<span class="function"> =&gt;</span>
        expresser.sockets.emit <span class="string">"server:connectionCounter"</span>, expresser.sockets.getConnectionCount()</pre></div>
        
      
        
        <p>When an admin user triggers the &quot;clients:refresh&quot; command, resend it
to all connected clients so they&#39;ll get the page refreshed.</p>

        
          <div class='highlight'><pre>    <span class="attribute">onClientsRefresh</span>: <span class="function"><span class="params">(data)</span> =&gt;</span>
        expresser.sockets.emit <span class="string">"clients:refresh"</span>, data</pre></div>
        
      
        
        <h2 id="singleton-implementation">Singleton implementation</h2>

        
      
        
        
        
          <div class='highlight'><pre>Sockets.<span class="function"><span class="title">getInstance</span> = -&gt;</span>
    <span class="property">@instance</span> = <span class="keyword">new</span> Sockets() <span class="keyword">if</span> <span class="keyword">not</span> <span class="property">@instance</span>?
    <span class="keyword">return</span> <span class="property">@instance</span>

module.<span class="built_in">exports</span> = <span class="built_in">exports</span> = Sockets.getInstance()</pre></div>
        
      
      <div class="fleur">h</div>
    </div>
  </div>
</body>
</html>
